package com.ssfGame.puremvc.view.application
{
	
	import com.game.utils.GlobalFun;
	import com.ssfGame.puremvc.StaticConst;
	import com.ssfGame.puremvc.view.vc.PopupLayerVC;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	/**
	 * 
	 * @author chery
	 */
	public class PopupLayerMediator extends Mediator
	{
		private var _list:Array=[];
		/**
		 * 
		 * @default 
		 */
		public static const NAME:String="PopupLayerMediator"
		/**
		 * 
		 * @param mediatorName
		 * @param viewComponent
		 */
		public function PopupLayerMediator(mediatorName:String=null, viewComponent:Object=null)
		{
			super(mediatorName, viewComponent);
		}
		override public function listNotificationInterests():Array{
			return [
				StaticConst.N_M_POPUP_LAYER_ADD,
				StaticConst.N_M_POPUP_LAYER_DEL
			]
		}
		override public function handleNotification(notification:INotification):void{
			var disObj:DisplayObject=notification.getBody() as DisplayObject;
			var _type:String=notification.getType() as String;
			var _isSetCenter:Boolean=true;
			if(_type=="false"){
				_isSetCenter=false;
			}
			if(disObj==null){
				return;
			}
			switch(notification.getName()){
				case StaticConst.N_M_POPUP_LAYER_ADD:
					
					addDisObj(disObj,_isSetCenter);
					break
				case StaticConst.N_M_POPUP_LAYER_DEL:
					
					delDisObj(disObj);
					break
			}
		}
		
		override public function onRegister():void
		{
			super.onRegister();
			
		}
		
		public function get popupLayerVC():PopupLayerVC{
			return viewComponent as PopupLayerVC;
		}
		
		
		private function addDisObj(disObj:DisplayObject,isSetCenter:Boolean=true):void{
			if(isSetCenter){
				GlobalFun.setDisCenter(disObj,StaticConst.appStage);
			}
			var _sp:Sprite=addAlertBg(disObj);
			popupLayerVC.addChild(_sp);
			_list.push({sp:_sp,disObj:disObj,isSetCenter:isSetCenter});
		}
		private function delDisObj(disObj:DisplayObject):void{
			for(var i:uint=0;i<_list.length;i++){
				if(disObj==_list[i].disObj){
					popupLayerVC.removeChild(_list[i].sp);
					_list[i].sp=null;
					_list[i].disObj=null;
					var _Obj:Object=_list.splice(i,1);
					break;
				}
			}
		}
		/**
		 * 弹出层添加半透明背景
		 * @param disObj 显示对象
		 * @return 
		 */
		private function addAlertBg(disObj:DisplayObject=null):Sprite{
			var _sp:Sprite=new Sprite();
			var _bg:Sprite=new Sprite();
			_sp.addChild(_bg);
			if(disObj!=null){
				_sp.addChild(disObj);
			}
			//_bg.graphics.beginFill(0,0.1);
			_bg.graphics.beginFill(0,0);
			_bg.graphics.drawRect(0,0,StaticConst.appStage.stageWidth,StaticConst.appStage.stageWidth);
			_bg.graphics.endFill();
			return _sp;
		}
	}
}